package com.example.springbootquartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.springbootquartz.common.ResponseVo;
import com.example.springbootquartz.entity.ScheduleJob;
import com.example.springbootquartz.service.IQuartzJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/sys/quartzJob")
@Slf4j
public class QuartzJobController {
	@Autowired
	private IQuartzJobService quartzJobService;
	@Autowired
	private Scheduler scheduler;

	/**
	 * 分页列表查询
	 * 
	 * @param quartzJob
	 * @param pageNo
	 * @param pageSize
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResponseVo<?> queryPageList(ScheduleJob quartzJob, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		LambdaQueryWrapper<ScheduleJob> queryWrapper = Wrappers.lambdaQuery(ScheduleJob.class);
		Page<ScheduleJob> page = new Page<ScheduleJob>(pageNo, pageSize);
		IPage<ScheduleJob> pageList = quartzJobService.page(page, queryWrapper);
        return ResponseVo.ok(pageList);

	}

	/**
	 * 添加定时任务
	 * 
	 * @param quartzJob
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResponseVo<?> add(@RequestBody ScheduleJob quartzJob) {
		quartzJobService.saveScheduleJob(quartzJob);
		return ResponseVo.ok("创建定时任务成功");
	}

	/**
	 * 更新定时任务
	 * 
	 * @param quartzJob
	 * @return
	 */
	@RequestMapping(value = "/edit", method ={RequestMethod.PUT, RequestMethod.POST})
	public ResponseVo<?> eidt(@RequestBody ScheduleJob quartzJob) {
		try {
			quartzJobService.editScheduleJob(quartzJob);
		} catch (SchedulerException e) {
			log.error(e.getMessage(),e);
			return ResponseVo.error("更新定时任务失败!");
		}
	    return ResponseVo.ok("更新定时任务成功!");
	}

	/**
	 * 通过id删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public ResponseVo<?> delete(@RequestParam(name = "id", required = true) String id) {
		ScheduleJob quartzJob = quartzJobService.getById(id);
		if (quartzJob == null) {
			return ResponseVo.error("未找到对应实体");
		}
		quartzJobService.deleteStopJob(quartzJob);
        return ResponseVo.ok("删除成功!");

	}

	/**
	 * 暂停定时任务
	 * 
	 * @param id
	 * @return
	 */
	//@RequiresRoles("admin")
   // @RequiresPermissions("system:quartzJob:pause")
	@GetMapping(value = "/pause")
	public ResponseVo<Object> pauseJob(@RequestParam(name = "id") String id) {
		ScheduleJob job = quartzJobService.getById(id);
		if (job == null) {
			return ResponseVo.error("定时任务不存在！");
		}
		quartzJobService.pause(job);
		return ResponseVo.ok("停止定时任务成功");
	}

	/**
	 * 启动定时任务
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/resume")
	public ResponseVo<Object> resumeJob(@RequestParam(name = "id") String id) {
		ScheduleJob job = quartzJobService.getById(id);
		if (job == null) {
			return ResponseVo.error("定时任务不存在！");
		}
		quartzJobService.resumeJob(job);
		//scheduler.resumeJob(JobKey.jobKey(job.getJobClassName().trim()));
		return ResponseVo.ok("启动定时任务成功");
	}

	/**
	 * 通过id查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryById", method = RequestMethod.GET)
	public ResponseVo<?> queryById(@RequestParam(name = "id", required = true) String id) {
		ScheduleJob quartzJob = quartzJobService.getById(id);
        return ResponseVo.ok(quartzJob);
	}



	/**
	 * 立即执行
	 * @param id
	 * @return
	 */
	@GetMapping("/execute")
	public ResponseVo<?> execute(@RequestParam(name = "id", required = true) String id) {
		ScheduleJob quartzJob = quartzJobService.getById(id);
		if (quartzJob == null) {
			return ResponseVo.error("未找到对应实体");
		}
		try {
			quartzJobService.execute(quartzJob);
		} catch (Exception e) {
			//e.printStackTrace();
			log.info("定时任务 立即执行失败>>"+e.getMessage());
			return ResponseVo.error("执行失败!");
		}
		return ResponseVo.ok("执行成功!");
	}

}
